// This file is part of OpenTSDB.
// Copyright (C) 2017 TeleDB Development Group.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.utils;

import net.opentsdb.core.Const;

/**
 * description:   时间处理相关类
 * author: zhuzhipeng
 * create: 2019-05-16 14:38
 */
public class TimestampUtils {

    public static boolean isMills(long timestamp){
        return (timestamp&Const.MILLISECOND_MASK) == 0;
    }

    public static boolean isSeconds(long timestamp){
        return (timestamp&Const.SECOND_MASK) == 0;
    }

    public static long millsToSeconds(long timestamp){
        if(isMills(timestamp)){
            return timestamp/1000;
        }else {
            return timestamp;
        }
    }

    /**
     * 时间向下取整，以小时为单位
     *
     * @param timestamp
     * @return  timeMills
     */
    public static long floorHours(long timestamp){
        Assert.operation(isMills(timestamp)||isSeconds(timestamp),
                "time "+timestamp+" is illegal,");
        long seconds = millsToSeconds(timestamp);

        return (seconds - (seconds % Const.MAX_TIMESPAN)) * 1000;
    }

    /**
     * 时间向上取整，以小时为单位
     *
     * @param timestamp
     * @return
     */
    public static long ceilHours(long timestamp){
        return floorHours(timestamp)+Const.MAX_TIMESPAN * 1000;
    }

}

